﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

namespace ZeeiCommon.Telnet
{
    public class TelnetBSC
    {
        //public ILog LOGGER = LogManager.GetLogger("AutoMonitor.Logging");
        const int COMMANDTIMEOUTSECONDS = 60;
        //Model.BscConfig bscModel = new Model.BscConfig();
        public string Address;
        public int Port;
        public string UserName;
        public string Password;

        public TelnetBSC()
        {

        }
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="bscModel"></param>
        /// <returns></returns>
        public ScriptingTelnet ConnectBSC()
        {
            int CommandTimeoutSeconds = COMMANDTIMEOUTSECONDS;
            ScriptingTelnet telnet = new ScriptingTelnet(Address, Port, CommandTimeoutSeconds);
            if (telnet.Connect())
            {
                telnet.SendMessage(UserName);
                if (telnet.WaitAndSend("PASSWORD", Password))
                {
                        return telnet;
                }
                else
                {
                    telnet = null;
                }
            }
            return telnet;
        }
        /// <summary>
        /// 发送命令
        /// </summary>
        /// <param name="telnet"></param>
        /// <param name="command"></param>
        /// <returns></returns>
        public int SendCommand(ScriptingTelnet telnet, string command)
        {
            int result = 1;//1
            string log = "";
            string waitingForString = WaitingForString(command);
            for (int i = 0; i < 5; i++)
            {
                telnet.ClearSessionLog();
                if (!telnet.IsConnected())
                {
                    telnet = ConnectBSC();
                }
                if (telnet.SendAndWait(command, waitingForString))
                {
                    if (waitingForString == "?")
                    {
                        telnet.SendAndWait("Y", "<");
                    }
                    result = 1;

                }
                else
                {
                    result = 0;
                }

                log = telnet.SessionLog;
            }

            return result;
        }



        /// <summary>
        /// 发送命令
        /// </summary>
        /// <param name="telnet"></param>
        /// <param name="command"></param>
        /// <returns></returns>
        public int SendCommand_NoWait(ScriptingTelnet telnet, string command)
        {
            int result = 1;//1
            string log = "";
            
                telnet.ClearSessionLog();
                if (!telnet.IsConnected())
                {
                    telnet = ConnectBSC();
                }
                if (telnet.SendMessage(command))
                {
                    result = 1;
                }
                else
                {
                    result = 0;
                }
                log = telnet.SessionLog;
                return result;
        }


        public string WaitingForString(string command)
        {
            string result = "<";
            switch (command.Substring(0, 4))
            {
                case "ZESG": result = "?"; break;
                case "ZUJR": result = "?"; break;
                case "ZAHP": result = "<A"; break;
                case "ZEOH": result = "<E"; break;
                case "ZDDS": result = ">"; break;
                case "ZDDE": result = ">"; break;
                case "ZRS:": result = ">"; break;
                case "dpcu": result = ">"; break;
                case "exit": result = ">"; break;
                case "ZESE": result = "POOL CREATED"; break;
                default: break;
            }
            return result;

        }
        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="telnet"></param>
        public void DisconnectBSC(ScriptingTelnet telnet)
        {
            telnet.Disconnect();
        }


    }
}
